#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Check if user names in data file are correctly saved
"""

import json
import os

def check_user_names():
    """Check user names in data file"""
    users_file = os.path.join('gt_lms', 'data', 'users.json')
    
    if not os.path.exists(users_file):
        print(f"User data file does not exist: {users_file}")
        return
    
    try:
        with open(users_file, 'r', encoding='utf-8') as f:
            users_data = json.load(f)
        
        print(f"User data file loaded successfully, total {len(users_data)} users")
        
        # Check several users' names
        test_user_ids = ['student_001', 'student_002', 'student_003', 'teacher_001', 'admin_001']
        
        for user_id in test_user_ids:
            if user_id in users_data:
                user_data = users_data[user_id]
                if isinstance(user_data, dict) and 'name' in user_data:
                    print(f'{user_id}: {user_data["name"]}')
                else:
                    print(f'{user_id}: Incorrect data format')
            else:
                print(f'{user_id}: Not found')
                
        # Check for users with truncated names
        truncated_users = []
        for user_id, user_data in users_data.items():
            if isinstance(user_data, dict) and 'name' in user_data:
                name = user_data['name']
                if '测试学生' in name and len(name) < len('测试学生') + 3:
                    truncated_users.append((user_id, name))
        
        if truncated_users:
            print("\nPotentially truncated users:")
            for user_id, name in truncated_users:
                print(f"  {user_id}: {name}")
        else:
            print("\nNo obviously truncated user names found")
            
    except Exception as e:
        print(f"Error during check: {e}")

if __name__ == "__main__":
    check_user_names()